package com.mqz.zebraEcommerce.commodity.service.impl;

import com.mqz.zebraEcommerce.commodity.vo.CategoryLevel2VO;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mqz.zebraEcommerce.common.utils.PageUtils;
import com.mqz.zebraEcommerce.common.utils.Query;

import com.mqz.zebraEcommerce.commodity.dao.CategoryDao;
import com.mqz.zebraEcommerce.commodity.entity.CategoryEntity;
import com.mqz.zebraEcommerce.commodity.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listTree() {

        List<CategoryEntity> entities = baseMapper.selectList(null);
        List<CategoryEntity> categoryTree = entities.stream().filter(categoryEntity -> {
            return categoryEntity.getParentId() == 0;
        }).map(categoryEntity -> {
            categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity, entities));

            return categoryEntity;
        }).sorted((categoryEntity1, categoryEntity2) -> {
            return (categoryEntity1.getSort() == null ? 0 : categoryEntity1.getSort()) - (categoryEntity2.getSort() == null ? 0 : categoryEntity2.getSort());
        }).collect(Collectors.toList());
        return categoryTree;
    }

    //递归查询所有分类的子分类
    private List<CategoryEntity> getChildrenCategories(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> list = all.stream().filter(categoryEntity -> {
            //过滤出一级分类的子分类
            return categoryEntity.getParentId().equals(root.getId());
        }).map(categoryEntity -> {
            categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity, all));
            return categoryEntity;
        }).sorted((categoryEntity1, categoryEntity2) -> {
            return (categoryEntity1.getSort() == null ? 0 : categoryEntity1.getSort()) - (categoryEntity2.getSort() == null ? 0 : categoryEntity2.getSort());
        }).collect(Collectors.toList());
        return list;
    }

    private List<Long> getParentCategoryId(Long categoryId, List<Long> cascadedCategoryId) {
        cascadedCategoryId.add(categoryId);
        CategoryEntity categoryEntity = this.getById(categoryId);

        if (!categoryEntity.getParentId().equals(0L)) {
            this.getParentCategoryId(categoryEntity.getParentId(), cascadedCategoryId);
        }
        return cascadedCategoryId;
    }

    @Override
    public Long[] getCascadedCategoryId(Long categoryId) {
        List<Long> cascadedCategoryId = new ArrayList<>();
        getParentCategoryId(categoryId, cascadedCategoryId);
        Collections.reverse(cascadedCategoryId);

        return cascadedCategoryId.toArray(new Long[cascadedCategoryId.size()]);
    }

    @Override
    public List<CategoryEntity> getCategoriesLevel1() {
        return baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("cat_level", 1));
    }

    @Override
    public Map<String, List<CategoryLevel2VO>> getCategoriesLevel2() {

        List<CategoryEntity> all = baseMapper.selectList(null);
        List<CategoryEntity> level1 = getCategoriesByParentId(all, 0L);

        return level1.stream().collect(Collectors.toMap(
                k -> k.getId().toString(),
                v -> {
                    List<CategoryLevel2VO> categoryLevel2VOs = new ArrayList<>();

                    List<CategoryEntity> categoryLevel2Entities = getCategoriesByParentId(all, v.getId());
                    if (categoryLevel2Entities != null && categoryLevel2Entities.size() > 0) {
                        categoryLevel2VOs = categoryLevel2Entities.stream().map(c2 -> {
                            CategoryLevel2VO categoryLevel2VO = new CategoryLevel2VO(v.getId().toString(), c2.getId().toString(), c2.getName(), null);

                            List<CategoryEntity> categoryLevel3Entities = getCategoriesByParentId(all, c2.getId());
                            if (categoryLevel3Entities != null && categoryLevel3Entities.size() > 0) {
                                List<CategoryLevel2VO.CategoryLevel3VO> categoryLevel3VOs = categoryLevel3Entities.stream().map(
                                        c3 -> new CategoryLevel2VO.CategoryLevel3VO(c2.getId().toString(), c3.getId().toString(), c3.getName())).collect(Collectors.toList());
                                categoryLevel2VO.setCatalog3List(categoryLevel3VOs);
                            }
                            return categoryLevel2VO;
                        }).collect(Collectors.toList());
                    }
                    return categoryLevel2VOs;
                }
        ));

    }

    //根据
    private List<CategoryEntity> getCategoriesByParentId(List<CategoryEntity> selectList, Long parentId) {
        return selectList.stream().filter(item -> item.getParentId().equals(parentId)).collect(Collectors.toList());
    }
}